package com.cspg.ssss.service;

import com.alibaba.fastjson.JSON;
import com.cspg.ssss.domain.*;
import com.cspg.ssss.repository.ScoreStatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class ScoreStatisticsService {

    @Autowired
    private ScoreStatisticsMapper scoreStatisticsMapper;

    public List<ScoreVO> selectScoreList(Map<String, Object> map) {
        List<ScoreVO> list = new ArrayList<>();
        List<ScoreSelectVO> subVOs;
        if (Integer.parseInt(map.get("type").toString()) == 0) {
            // 查询单位的减分问题集合
            subVOs = scoreStatisticsMapper.selectGsSubScoreList(map);
        } else {
            // 查询部门的减分问题集合
            subVOs = scoreStatisticsMapper.selectDwSubScoreList(map);
        }
        for (ScoreSelectVO subVo : subVOs) {
            ScoreVO scoreVO = new ScoreVO();
            if (!StringUtils.isEmpty(subVo.getDwmc())) {
                scoreVO.setDwmc(subVo.getDwmc());
                scoreVO.setDeptName(subVo.getDwmc());
            }
            if (!StringUtils.isEmpty(subVo.getDeptName()))
                scoreVO.setDeptName(subVo.getDeptName().replaceAll("超高压输电公司/", ""));
            int upScore = 0; //上半年减分总和
            int downScore = 0;//下半年减分总和
            if (subVo.getSubList().size() != 0) {
                for (Map<String, String> data : subVo.getSubList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            scoreVO.setUpSub((float) upScore / 100);
            scoreVO.setUpTotal((float) upScore / 100);
            scoreVO.setDownSub((float) downScore / 100);
            scoreVO.setDownTotal((float) downScore / 100);
            list.add(scoreVO);
        }
        List<ScoreSelectVO> addVOs = scoreStatisticsMapper.selectAddScoreList(map);
        for (ScoreSelectVO addVO : addVOs) {
            ScoreVO scoreVO = new ScoreVO();
            String deptName = addVO.getDeptName().replaceAll("超高压输电公司/", "");
            if (Integer.parseInt(map.get("type").toString()) == 0) {
                scoreVO.setDwmc(deptName.contains("/") ? deptName.split("/")[0] : deptName);
            } else {
                scoreVO.setDeptName(deptName);
            }
            int upScore = 0;//上半年的加分总和
            int downScore = 0;//下半年的加分总和
            if (addVO.getAddList().size() != 0) {
                for (Map<String, String> data : addVO.getAddList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            scoreVO.setUpAdd((float) upScore / 100);
            scoreVO.setDownAdd((float) downScore / 100);
            scoreVO.setUpTotal((float) upScore / 100);
            scoreVO.setDownTotal((float) downScore / 100);
            boolean con = true;
            for (ScoreVO vo : list) {
                //与减分的单位或部门信息进行对接
                if (!StringUtils.isEmpty(scoreVO.getDwmc()) && scoreVO.getDwmc().equals(vo.getDwmc())) {
                    vo.setUpAdd(scoreVO.getUpAdd());
                    vo.setDownAdd(scoreVO.getDownAdd());
                    vo.setUpTotal(vo.getUpTotal() + scoreVO.getUpTotal());
                    vo.setDownTotal(vo.getDownTotal() + scoreVO.getDownTotal());
                    vo.setTotal(vo.getDownTotal() + vo.getUpTotal());
                    con = false;
                    break;
                }
                if (!StringUtils.isEmpty(scoreVO.getDeptName()) && scoreVO.getDeptName().equals(vo.getDeptName())) {
                    vo.setUpAdd(scoreVO.getUpAdd());
                    vo.setDownAdd(scoreVO.getDownAdd());
                    vo.setUpTotal(vo.getUpTotal() + scoreVO.getUpTotal());
                    vo.setDownTotal(vo.getDownTotal() + scoreVO.getDownTotal());
                    con = false;
                    break;
                }
            }
            if (con) {
                list.add(scoreVO);
            }
        }
        for (ScoreVO vo : list) {
            vo.setTotal((vo.getUpTotal() * 100 + vo.getDownTotal() * 100) / 100);
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateScoreStatisticsList(int type) {
        List<ScoreVO> list = new ArrayList<>();
        List<ScoreSelectVO> subVOs;
        Map<String, Object> con = new HashMap<>();
        if (type == 0) {
            // 查询单位的减分问题集合
            subVOs = scoreStatisticsMapper.selectGsSubScoreList(con);
        } else {
            // 查询部门的减分问题集合
            subVOs = scoreStatisticsMapper.selectDwSubScoreList(con);
        }
        for (ScoreSelectVO subVo : subVOs) {
            ScoreVO scoreVO = new ScoreVO();
            scoreVO.setType(type + "");
            scoreVO.setYear(subVo.getYear());
            scoreVO.setDwmc(subVo.getDwmc());
            scoreVO.setDeptName(subVo.getDeptName());
            int upScore = 0; //上半年减分总和
            int downScore = 0;//下半年减分总和
            if (subVo.getSubList().size() != 0) {
                for (Map<String, String> data : subVo.getSubList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            scoreVO.setUpSub((float) upScore / 100);
            scoreVO.setUpTotal((float) upScore / 100);
            scoreVO.setDownSub((float) downScore / 100);
            scoreVO.setDownTotal((float) downScore / 100);
            //使用年份，type，单位名称，部门名称，审核部门名称进行字符串组合
            String str = scoreVO.getYear() + scoreVO.getType() + scoreVO.getDwmc() + scoreVO.getDeptName();
            //加密生成唯一id
            scoreVO.setId(DigestUtils.md5DigestAsHex(str.getBytes()));
            list.add(scoreVO);
        }
        //根据条件map.get("type")查询单位或部门的加分事项
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        List<ScoreSelectVO> addVOs = scoreStatisticsMapper.selectAddScoreList(map);
        for (ScoreSelectVO addVO : addVOs) {
            ScoreVO scoreVO = new ScoreVO();
            scoreVO.setType(type + "");
            scoreVO.setYear(addVO.getYear());
            scoreVO.setDeptName(addVO.getDeptName());
            scoreVO.setDwmc(scoreVO.getDeptName().split("/")[1]);
            int upScore = 0;//上半年的加分总和
            int downScore = 0;//下半年的加分总和
            if (addVO.getAddList().size() != 0) {
                for (Map<String, String> data : addVO.getAddList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            scoreVO.setUpAdd((float) upScore / 100);
            scoreVO.setDownAdd((float) downScore / 100);
            scoreVO.setUpTotal((float) upScore / 100);
            scoreVO.setDownTotal((float) downScore / 100);
            //使用年份，type，单位名称，部门名称，审核部门名称进行字符串组合
            String str = scoreVO.getYear() + scoreVO.getType() + scoreVO.getDwmc() + scoreVO.getDeptName();
            //加密生成唯一id
            scoreVO.setId(DigestUtils.md5DigestAsHex(str.getBytes()));
            for (ScoreVO vo : list) {
                //与减分的单位或部门信息进行对接
                if (scoreVO.getId().equals(vo.getId())) {
                    vo.setUpAdd(scoreVO.getUpAdd());
                    vo.setDownAdd(scoreVO.getDownAdd());
                    vo.setUpTotal(vo.getUpTotal() + scoreVO.getUpTotal());
                    vo.setDownTotal(vo.getDownTotal() + scoreVO.getDownTotal());
                    break;
                }
            }
        }
        for (ScoreVO vo : list) {
            if (vo.getAddList() != null && vo.getAddList().size() > 0) {
                vo.setAddListJson(JSON.toJSONString(vo.getAddList()));
            }
            if (vo.getSubList() != null && vo.getSubList().size() > 0) {
                vo.setSubListJson(JSON.toJSONString(vo.getSubList()));
            }
            vo.setTotal(vo.getDownTotal() + vo.getUpTotal());
            if (scoreStatisticsMapper.selectScoreStatisticsById(vo.getId()) > 0) {
                scoreStatisticsMapper.updateScoreStatistics(vo);
            } else {
                scoreStatisticsMapper.insertScoreStatistics(vo);
            }
        }
    }

    public List<ScoreDetailedVO> scoreGsDetailedList(Map<String, Object> map) {
        List<ScoreDetailedVO> list = new ArrayList<>();
        if (Integer.parseInt(map.get("score_type").toString()) == 0) {
            List<GsdgdwGradeViewEntity> gsList = scoreStatisticsMapper.selectGsGradeList(map);
            for (GsdgdwGradeViewEntity entity : gsList) {
                ScoreDetailedVO vo = new ScoreDetailedVO();
                vo.setId(entity.getId());
                vo.setBmmc(entity.getDwmc());
                entity.setGrade_dept(entity.getGrade_dept().replaceAll("->", "/"));
                vo.setKhbm(entity.getGrade_dept().replaceAll("超高压输电公司/", ""));
                vo.setTitle(entity.getWtms());
                vo.setGrade_time(entity.getGrade_time());
                vo.setUsername(entity.getCheck_username());
                vo.setScore(entity.getScore());
                vo.setCheck_type(0);
                vo.setType(0);
                list.add(vo);
            }
        } else if (Integer.parseInt(map.get("score_type").toString()) == 1) {
            map.put("check_type",0);
            List<ScoreAddApplyEntity> addList = scoreStatisticsMapper.selectAddGradeList(map);
            for (ScoreAddApplyEntity entity : addList) {
                ScoreDetailedVO vo = new ScoreDetailedVO();
                vo.setId(entity.getId());
                vo.setBmmc(entity.getApply_dept().replaceAll("超高压输电公司/", ""));
                vo.setKhbm(entity.getAsse_dept().replaceAll("超高压输电公司/", ""));
                vo.setTitle(entity.getAdd_reson());
                vo.setGrade_time(entity.getApply_time());
                vo.setUsername(entity.getUser_name());
                vo.setCheck_type(entity.getApply_type());
                vo.setScore(entity.getScore());
                vo.setType(1);
                list.add(vo);
            }
        }
        return list;
    }

    public List<ScoreDetailedVO> scoreDwDetailedList(Map<String, Object> map) {
        List<ScoreDetailedVO> list = new ArrayList<>();
        List<DwdgbmGradeViewEntity> dwList = scoreStatisticsMapper.selectDwGradeList(map);
        for (DwdgbmGradeViewEntity entity : dwList) {
            ScoreDetailedVO vo = new ScoreDetailedVO();
            vo.setId(entity.getId());
            vo.setBmmc(entity.getBmmc().replaceAll("->", "/"));
            entity.setGrade_dept(entity.getGrade_dept().replaceAll("->", "/"));
            vo.setKhbm(entity.getGrade_dept().replaceAll("超高压输电公司/", ""));
            vo.setTitle(entity.getWtms());
            vo.setGrade_time(entity.getGrade_time());
            vo.setUsername(entity.getCheck_username());
            vo.setScore(entity.getScore());
            vo.setCheck_type(1);
            vo.setType(0);
            list.add(vo);
        }
        map.put("check_type",1);
        List<ScoreAddApplyEntity> addList = scoreStatisticsMapper.selectAddGradeList(map);
        for (ScoreAddApplyEntity entity : addList) {
            ScoreDetailedVO vo = new ScoreDetailedVO();
            vo.setId(entity.getId());
            vo.setBmmc(entity.getApply_dept().replaceAll("超高压输电公司/", ""));
            vo.setKhbm(entity.getAsse_dept().replaceAll("超高压输电公司/", ""));
            vo.setTitle(entity.getAdd_reson());
            vo.setGrade_time(entity.getApply_time());
            vo.setUsername(entity.getUser_name());
            vo.setCheck_type(entity.getApply_type());
            vo.setScore(entity.getScore());
            vo.setType(1);
            list.add(vo);
        }
        return list;
    }


    public List<String> scoreDeptList(Map<String, Object> map) {
        List<String> subList;
        if (Integer.parseInt(map.get("type").toString()) == 0) {
            // 查询单位的减分问题集合
            subList = scoreStatisticsMapper.scoreGsDeptList(map);
        } else {
            // 查询部门的减分问题集合
            subList = scoreStatisticsMapper.scoreDwDeptList(map);
        }
        List<String> addList = scoreStatisticsMapper.scoreAddDeptList(map);
        Set<String> set = new HashSet<>();
        set.addAll(subList);
        set.addAll(addList);
        return new ArrayList<>(set);
    }

    public List<ScoreDetailedTotalVO> selectDetailedTotalList(Map<String, Object> map) {
        List<ScoreDetailedTotalVO> list = new ArrayList<>();
        List<ScoreDetailedTotalVO> subVOs = scoreStatisticsMapper.selectDetailedTotalList(map);
        for (ScoreDetailedTotalVO subVo : subVOs) {
            if (!StringUtils.isEmpty(subVo.getAuditDept())) {
                subVo.setAuditDept(subVo.getAuditDept().replaceAll("超高压输电公司/", ""));
            }
            int upScore = 0; //上半年减分总和
            int downScore = 0;//下半年减分总和
            if (subVo.getList().size() != 0) {
                for (Map<String, String> data : subVo.getList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore -= (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            subVo.setUpSub((float) upScore / 100);
            subVo.setUpTotal((float) upScore / 100);
            subVo.setDownSub((float) downScore / 100);
            subVo.setDownTotal((float) downScore / 100);
            list.add(subVo);
        }
        List<ScoreDetailedTotalVO> addVOs = scoreStatisticsMapper.selectAddDetailedTotalList(map);
        for (ScoreDetailedTotalVO addVO : addVOs) {
            if (!StringUtils.isEmpty(addVO.getAuditDept())) {
                addVO.setAuditDept(addVO.getAuditDept().replaceAll("超高压输电公司/", ""));
            }
            int upScore = 0;//上半年的加分总和
            int downScore = 0;//下半年的加分总和
            if (addVO.getList().size() != 0) {
                for (Map<String, String> data : addVO.getList()) {
                    //判断是上半年还是下半年，进行评分总和
                    if (Integer.parseInt(data.get("months")) < 7) {
                        upScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    } else {
                        downScore += (int) (Float.parseFloat(data.get("score")) * 100);
                    }
                }
            }
            addVO.setUpAdd((float) upScore / 100);
            addVO.setDownAdd((float) downScore / 100);
            addVO.setUpTotal((float) upScore / 100);
            addVO.setDownTotal((float) downScore / 100);
            boolean con = true;
            for (ScoreDetailedTotalVO vo : list) {
                //与减分的单位或部门信息进行对接
                if (addVO.getAuditDept().equals(vo.getAuditDept())) {
                    vo.setUpAdd(addVO.getUpAdd());
                    vo.setDownAdd(addVO.getDownAdd());
                    vo.setUpTotal(vo.getUpTotal() + addVO.getUpTotal());
                    vo.setDownTotal(vo.getDownTotal() + addVO.getDownTotal());
                    con = false;
                    break;
                }
            }
            if (con) {
                list.add(addVO);
            }
        }
        for (ScoreDetailedTotalVO vo : list) {
            vo.setTotal((vo.getUpTotal() * 100 + vo.getDownTotal() * 100) / 100);
        }
        return list;
    }
}
